using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using FAQComponents;
using FAQComponents.Objects;
using FAQComponents.API;
using Adge.UI.WebControls;

namespace Adge.Web.Faq.Admin
{
	/// <summary>
	/// Summary description for PermissionsAdmin.
	/// </summary>
	public partial class PermissionsAdmin : CategoryRepeaterPage
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			PermissionAPI.EnsureAdministrator();

			if (!Page.IsPostBack)
			{
				users.DataSource = UserAPI.GetAllUsers();
				users.DataBind();

				LoadPermissionsUI();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.rptAvailable.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.rptAvailable_ItemDataBound);
			this.rptAvailable.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler(this.rptAvailable_ItemCommand);
			this.rptCurrent.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.rptCurrent_ItemDataBound);
			this.rptCurrent.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler(this.rptCurrent_ItemCommand);

		}
		#endregion

		protected void users_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			LoadPermissionsUI();
		}

		CategoryCollection csAvailable, csCurrent;
		private void LoadPermissionsUI()
		{
			User u = UserAPI.GetUser(InvariantString.ToInt32(users.SelectedValue));

			messageForAdmins.Visible = u.IsAdmin;
			pnlChoosePermissions.Visible = !u.IsAdmin;

			if (!u.IsAdmin)
			{
				// == Get the Available and Current Permissions ==
				csAvailable = UserAPI.GetAvailablePermissions(u.UserID);
				csCurrent = u.Permissions;

				// == Get ALL Categories ==
				CategoryCollection allCats = CategoryAPI.GetAllCategories();
				allCats.Sort();

				rptAvailable.DataSource = allCats;
				rptAvailable.DataBind();

				rptCurrent.DataSource = allCats;
				rptCurrent.DataBind();
			}
		}

		#region rptAvailable Event Handlers
		private void rptAvailable_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
		{
			if (e.CommandName == "Add")
			{
				// determine the ID and the role
				int categoryID = InvariantString.ToInt32(e.CommandArgument.ToString());
				Roles roleID = (Roles) InvariantString.ToInt32(((DropDownList) e.Item.FindControl("role")).SelectedValue);
				int userID = InvariantString.ToInt32(users.SelectedValue);

				UserAPI.AddPermission(userID, categoryID, roleID);

				LoadPermissionsUI();
			}
		}

		private void rptAvailable_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				// determine if this category is in the user permissions
				int categoryID = (int) DataBinder.Eval(e.Item.DataItem, "CategoryID");
				bool userHasPermission = (csCurrent.FindCategory(categoryID) != null);

				// set the visibility of the LinkButton/DDL based on whether the user has this permission
				DropDownList role = (DropDownList) e.Item.FindControl("role");
				LinkButton btnAddPermission = (LinkButton) e.Item.FindControl("btnAddPermission");
				role.Visible = 	btnAddPermission.Visible = !userHasPermission;
			}
		}
		#endregion

		protected string DisplayPermission(int categoryID, string name)
		{
			Category p = csCurrent.FindCategory(categoryID);
			if (p != null)
			{
				return string.Concat("<span style=\"background-color: pink;\">", name, "</span> (") + string.Concat(p.UsersRole.ToString(), ")");
			}
			else
				return name;
		}

		#region rptCurrent Event Handlers
		private void rptCurrent_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				// determine if this category is in the user permissions
				int categoryID = (int) DataBinder.Eval(e.Item.DataItem, "CategoryID");
				bool userHasPermission = (csCurrent.FindCategory(categoryID) != null);

				// set the visibility of the LinkButton based on whether the user has this permission
				LinkButton lnkRemovePermission = (LinkButton) e.Item.FindControl("lnkRemovePermission");
				lnkRemovePermission.Visible = userHasPermission;
			}
		}

		private void rptCurrent_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
		{
			if (e.CommandName == "Remove")
			{
				// determine the ID and the role
				int categoryID = InvariantString.ToInt32(e.CommandArgument.ToString());
				int userID = InvariantString.ToInt32(users.SelectedValue);

				UserAPI.RemovePermission(userID, categoryID);

				LoadPermissionsUI();
			}
		}
		#endregion
	}
}
